<?php

namespace app\api\model;

use think\Model;
use think\Db;
/**
 * 用户模型
 */
class GoodsComment extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**
     * @param array $data
     * @return false|int|string
     * 添加评论
     */
    public function add($data = [])
    {
        if (!$data) return false;
        return Db::name('goods_comment')->insert($data);
    }

    public static function commentslist($id,$uid = '', $type = 'goods')
    {
        $where = [];
        $where['gid'] = ['eq',$id];
        $where['goods_type'] = ['eq',$type];
        if (empty($uid))
        {
            $where['status'] = ['eq',0];
            $response = Db::name('goods_comment')->field('weigh,update_time',true)->where($where)->select();
        }else{
            // 如果有uid 我先查询到所有的,然后判断
            $data = Db::name('goods_comment')->field('weigh,update_time',true)->where($where)->select();

            $response = [];
            foreach($data as $key => $val){
                if ($val['status'] == 1 && $val['uid'] != $uid)
                {
                    continue;
                }
                $response[] = $val;
            }
        }

        return $response;
    }

}
